home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / Icon 8.1 / mep1 / Samples / Programs / textcount.icn < prev    next >
Encoding:
Text File  |  1990-11-24  |  1.3 KB  |  49 lines  |  [TEXT/PICN]

  1. ############################################################################
  2. #
  3. #  textcount.icn
  4. #  
  5. #     This program tabulates the number of characters, "words", and
  6. #  lines in a file, and gives the lengths of the longest and shortest lines.
  7. #  The user is prompted with a Get File dialog and the program continues to
  8. #  process files until the user selects Cancel.
  9. #  
  10. ############################################################################
  11.  
  12. procedure main()
  13.    local chars, words, lines, name, infile, max, min, line
  14.  
  15.       while name := getfile("File?") do {
  16.       close(\infile)
  17.       infile := open(name) | {
  18.          write(&errout,"*** cannot open ",name)
  19.          next
  20.          }
  21.         chars := words := lines := 0
  22.       max := 0
  23.       min := 2 ^ 30                # larger than possible line length
  24.     
  25.       while line := read(infile) do {
  26.          max <:= *line
  27.          min >:= *line
  28.          lines +:= 1
  29.          chars +:= *line + 1
  30.          line ? while tab(upto(&letters)) do {
  31.             words +:= 1
  32.             tab(many(&letters))
  33.             }
  34.          }
  35.     
  36.       if min = 2 ^ 30 then
  37.          write("empty file")
  38.       else {
  39.          write("number of lines:     ",right(lines,8))
  40.          write("number of words:     ",right(words,8))
  41.          write("number of characters:",right(chars,8))
  42.          write()
  43.          write("longest line:        ",right(max,8))
  44.          write("shortest line:       ",right(min,8))
  45.          }
  46.       }
  47.  
  48. end
  49.